﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using AdinZ.Common;
using Aliyun.OpenServices.OpenStorageService;
using System.IO;
using Aliyun.OpenServices;
/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/2 12:39:04
*
* */
namespace Novel.Collection
{
    public partial class Download : Form
    {
        Thread thread = null;
        public Download()
        {
            InitializeComponent();
            ListView.CheckForIllegalCrossThreadCalls = false;
        }

        Sys.BLL.novel_article bll_art = new Sys.BLL.novel_article();
        Sys.BLL.novel_chapter bll_chap = new Sys.BLL.novel_chapter();
        ClientConfiguration clientConfig = new ClientConfiguration();
        ObjectMetadata metadata = new ObjectMetadata();

        private void button1_Click(object sender, EventArgs e)
        {
           thread=new Thread( GetArtList);
           thread.IsBackground = true;
           thread.Start();


        }

        private void GetArtList()
        {
            DataSet ds = bll_art.GetRemoteList();


            for (int count = 0; count < ds.Tables[0].Rows.Count;count++ )
            {
                string id = ds.Tables[0].Rows[count]["ID"].ToString();
                string cover = ds.Tables[0].Rows[count]["cover"].ToString();
                ListViewItem list = new ListViewItem(id);
                list.SubItems.Add(cover);
                list.SubItems.Add("?");
                listView1.Items.Add(list);
            }
            for (int count = 0; count < ds.Tables[0].Rows.Count; count++)
            {
                string id = ds.Tables[0].Rows[count]["ID"].ToString();
                string cover = ds.Tables[0].Rows[count]["cover"].ToString();

                if (!bll_art.ExistsCover(int.Parse(id)))
                {
                    #region 下载、更新、上传封面
                    DownloadFile df = new DownloadFile();
                    df.StrUrl = cover;
                    df.StrFileName = "cover/cover_" + id + ".jpg";
                    if (df.Download())
                    {
                        try
                        {
                            using (var fs = File.Open(df.StrFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                            {
                                metadata.ContentType = "image/jpeg";
                                if (OssUpload.Upload(df.StrFileName, fs, metadata, clientConfig))
                                {
                                    if (bll_art.UpdateCover(int.Parse(id), "cover/cover_" + id + ".jpg", 1) > 0)
                                        listView1.Items[count].SubItems[1].Text = "cover/cover_" + id + ".jpg";

                                }
                            }
                            File.Delete(df.StrFileName);
                        }
                        catch (Exception e)
                        {
                            AdinZ.Common.WriteLog write = new WriteLog("log/log.log");
                            write.WriteLine("出错了错误ID" + id + "错误日志:" + e + "\r\n\r\n");
                        }
                    }
                    #endregion
                }
                DataSet ds_chap = bll_chap.GetRemoteList(int.Parse(id));



                #region 章节
                listView2.Items.Clear();

                for (int chap_count = 0; chap_count < ds_chap.Tables[0].Rows.Count; chap_count++)
                {

                    string chap_id = ds_chap.Tables[0].Rows[chap_count]["ID"].ToString();
                    string chap_Path = ds_chap.Tables[0].Rows[chap_count]["articlepath"].ToString();
                    ListViewItem list = new ListViewItem(chap_id);
                    list.SubItems.Add(chap_Path);
                    listView2.Items.Add(list);
                }


                for (int chap_count = 0; chap_count < ds_chap.Tables[0].Rows.Count; chap_count++)
                {
                    string chap_id = ds_chap.Tables[0].Rows[chap_count]["ID"].ToString();
                    string chap_Path = ds_chap.Tables[0].Rows[chap_count]["articlepath"].ToString();
                    string chaptername = ds_chap.Tables[0].Rows[chap_count]["chaptername"].ToString();


                    #region 下载、更新、修改、上传章节

                    DownloadFile df = new DownloadFile();
                    df.StrUrl = chap_Path;
                    df.StrFileName = "chapter/" + chap_id + ".txt";
                    if (File.Exists(df.StrFileName)) { File.Delete(df.StrFileName); }
                    if (df.Download())
                    {

                        string str = File.ReadAllText(df.StrFileName,Encoding.Default); //获得整个文本
                        str = "\t"+chaptername+"\r\n\r\n" + str;

                        using (var sw = new StreamWriter(df.StrFileName, false, Encoding.Default))
                        {
                            sw.WriteLine(str);
                            sw.Close();//写入
                        }

                        try
                        {
                            using (var fs = File.Open(df.StrFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                            {
                                metadata.ContentType = "text/plain";
                                if(OssUpload.Upload(df.StrFileName, fs, metadata, clientConfig))
                                {
                                    if (bll_chap.UpdateChapterPath(int.Parse(chap_id), df.StrFileName, 1,df.lDownloadFile) > 0)
                                        listView2.Items[chap_count].SubItems[1].Text = df.StrFileName;
                                }
                            }
                            File.Delete(df.StrFileName);

                        }
                        catch(Exception e)
                        {
                            AdinZ.Common.WriteLog write = new WriteLog("log/log.log");
                            write.WriteLine("出错了错误章节ID" + chap_id + "错误日志:" + e + "\r\n\r\n");
                        }

                    }
                    #endregion
                }
                #endregion
                listView1.Items[count].SubItems[2].Text = "√";

            }
        }

        private void Download_Load(object sender, EventArgs e)
        {

        }
    }
}
